package com.xhui.vimarket.common.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import com.xhui.vimarket.common.utils.vo.PageInfo;

/**
 * JDBC操作类
 * .
 * 
 * @author zfz
 * @version Revision 1.0.0
 */
public class MyJdbcTemplate extends JdbcTemplate {
    protected final Log log = LogFactory.getLog(getClass());
    
    /**
     * 分页查询
     * .
     * 
     * @param sql
     * @param args
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public PageInfo queryForListByPage(String sql, Object[] args,
        int pageIndex, int pageSize) {
        PageInfo pageInfo = new PageInfo();
        if (pageIndex <= 1) {
            pageIndex = 1;
        }
        if (pageSize <= 1) {
            pageSize = 10;
        }
        int from = (pageIndex - 1) * pageSize;
        if (from < 0) {
            from = 0;
        }
        int to = from + pageSize;
        
        /*String countSql = sql;
        countSql = countSql.replaceAll("FROM", "from");
        countSql = countSql.replaceAll("ORDER BY", "order by");
        
        int start = countSql.indexOf("from");
        int end = countSql.indexOf("order by");
        if (end > 1) {
            countSql = "select count(*) " + countSql.substring(start, end);
        } else {
            countSql = "select count(*) " + countSql.substring(start);
        }
        int resultCount = this.queryForInt(countSql, args);*/
                
        StringBuffer countSql = new StringBuffer();
        countSql.append("select count(*) from (");
        countSql.append(sql);
        countSql.append(") t");
        int resultCount = this.queryForInt(countSql.toString(), args);
        
        pageInfo.setTotalCount(resultCount);
        
        int pageCount = resultCount / pageSize;
        pageCount = (resultCount % pageSize) > 0 ? (pageCount + 1)
            : pageCount;
        pageInfo.setTotalPageCount(pageCount);
        
        StringBuffer selectSql = new StringBuffer();
        selectSql.append("select * from (select rownum rn,t.* from(");
        selectSql.append(sql);
        selectSql.append(") t where rownum<=");
        selectSql.append(to);
        selectSql.append(") where rn>");
        selectSql.append(from);
        
        log.debug("selectSql:" + selectSql.toString());
        List dataList = this.queryForList(selectSql.toString(), args);
        
        pageInfo.setDataList(dataList);
        pageInfo.setCurrentPage(pageIndex);
        pageInfo.setPerPageCount(pageSize);
        
        return pageInfo;
    }
    
    /**
     * 分页查询
     * .
     * 
     * @param sql
     * @param args
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public PageInfo queryForListByPageWithOutTotal(String sql, Object[] args,
        int pageIndex, int pageSize, int totalSize) {
        PageInfo pageInfo = new PageInfo();
        if (pageIndex <= 1) {
            pageIndex = 1;
        }
        if (pageSize <= 1) {
            pageSize = 10;
        }
        int from = (pageIndex - 1) * pageSize;
        if (from < 0) {
            from = 0;
        }
        int to = from + pageSize;
        
        StringBuffer selectSql = new StringBuffer();
        selectSql.append("select * from (select rownum rn,t.* from(");
        selectSql.append(sql);
        selectSql.append(") t where rownum<=");
        selectSql.append(to);
        selectSql.append(") where rn>");
        selectSql.append(from);
        
        log.debug("selectSql:" + selectSql.toString());
        List dataList = this.queryForList(selectSql.toString(), args);
        
        int totalPages = totalSize / pageSize;
        if ((totalSize % pageSize) > 0) {
            totalPages += 1;
        }
        if (pageIndex < totalPages) {
            pageInfo.setTotalCount(totalSize);
        } else {
            pageInfo.setTotalCount(totalPages == 0 ? dataList.size() + 1 : (totalSize + dataList.size()));
        }
        
        pageInfo.setDataList(dataList);
        pageInfo.setCurrentPage(pageIndex);
        pageInfo.setPerPageCount(pageSize);
        
        return pageInfo;
    }
}
